package com.lindroy.jetpacknotes.navigation.deeplink

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.navigation.NavDeepLinkBuilder
import com.lindroy.jetpacknotes.R
import kotlinx.android.synthetic.main.activity_navigation_deeplink_pending_intent.*


/**
 * @author Lin
 * @date 2022/1/3
 * @function 隐式 Deeplink，使用 PendingIntent
 * @Description
 */
class NavigationDeeplinkPendingIntentActivity : AppCompatActivity() {

    private val pendingIntent by lazy {
        NavDeepLinkBuilder(this)
            .setGraph(R.navigation.nav_graph_ui)
            .setDestination(R.id.mineFragment)
//            .setArguments(args)
            .setComponentName(NavigationDeeplinkPendingIntentActivity::class.java)
            .createPendingIntent()

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigation_deeplink_pending_intent)
        val channelId = "normal"
        val noticeManager = getSystemService(Context.NOTIFICATION_SERVICE)
            as NotificationManager
        //判断是否是Android8.0之后的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建通知渠道
            val channel = NotificationChannel(
                channelId,
                "Normal",   //对应在手机的通知设置页面的通知名称
                NotificationManager.IMPORTANCE_DEFAULT
            )
            noticeManager.createNotificationChannel(channel)
        }
        //点击按钮发送通知
        btnSendNotice.setOnClickListener {
            //设置通知样式，图标、标题等
            val notification = NotificationCompat.Builder(this, channelId)
                .setContentIntent(pendingIntent)
                .setContentTitle("Navigation-Deeplink")
                .setContentText("点击跳转")
                .setSmallIcon(R.drawable.ic_small)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_large))
                .build()
            //弹出通知
            noticeManager.notify(1, notification)
        }
    }
}